﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using cecbData;
using System.Data;
using cecbClasses.Data_Adapters;
using cecbClasses.Data_Adapters.Project;
using System.Collections.ObjectModel;
using System.Windows.Media.Animation;
namespace CECB.Impact
{
    /// <summary>
    /// Interaction logic for Project_Impact_Insert_Window.xaml
    /// </summary>
    public partial class Project_Impact_Insert_Window : Window
    {

        public ObservableCollection<Impacts> ModelListe { get; set; }
        Impacts model;

        private DSSEntities dataContext;
        private bool isprojectSelected = false;

        public Project_Impact_Insert_Window()
        {
            InitializeComponent();

            isprojectSelected = false;
            ModelListe = new ObservableCollection<Impacts>();
            DataContext = this;
            dataContext = new DSSEntities();
            cecbClasses.Data_Adapters.Slider slid = new cecbClasses.Data_Adapters.Slider();
            comboBoxComponents.ItemsSource = new DSSEntities().Components;
            ProjectAutocompleteTextBox.ItemsSource = slid.getProjectDetails();

            if (listBoxComponents.Items.Count > 0)
            {
                groupBox.Visibility = System.Windows.Visibility.Visible;
            }
            else groupBox.Visibility = System.Windows.Visibility.Hidden;

        }


        private void ProjectAutocompleteTextBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

            if (((AutoCompleteBox)sender).SelectedItem == null)
            {
                isprojectSelected = false;
                treeViewImpacts.Items.Clear();
                textBoxActivitySearch.Clear();
                textBoxAddActivity.Clear();
                return;

            }
            else
            {
                isprojectSelected = true;
                try
                {
                    treeViewImpacts.Items.Clear();
                    textBoxActivitySearch.Clear();
                    textBoxAddActivity.Clear();
                    cecbClasses.Data_Adapters.Slider slid = new cecbClasses.Data_Adapters.Slider();
                    isprojectSelected = true;
                    Projects proj = (Projects)ProjectAutocompleteTextBox.SelectedItem;
                    ModelListe.Clear();
                    ProjectDetailsWrapPanel.DataContext = slid.getProjectDetailsByRef(proj.proj_reference);
                    fillImpacts(proj.proj_reference);

                }
                catch (Exception)
                {

                }
            }

        }

        public void fillImpacts(String projRef)
        {

            //-------------------------------------------------------------------------------

            IQueryable<Components> qComponents = (from c in dataContext.ProjectImpacts
                                                  join b in dataContext.Impacts on c.impt_reference equals b.impt_reference
                                                  join d in dataContext.Components on b.impt_component equals d.cmpt_reference
                                                  where c.proj_reference == projRef
                                                  orderby b.impt_component
                                                  select d).Distinct();

            int y = 0;
            foreach (Components com in qComponents)
            {

                treeViewImpacts.Items.Add(new TreeViewItem()
                {
                    Header = com.cmpt_name //,
                    //   FontStyle = FontStyles.Italic,
                    //  FontWeight = FontWeights.Bold 
                });

                IQueryable<Impacts> qImpacts = from c in dataContext.ProjectImpacts
                                               join b in dataContext.Impacts on c.impt_reference equals b.impt_reference

                                               where c.proj_reference == projRef && b.impt_component == com.cmpt_reference
                                               orderby b.impt_component
                                               select b;


                foreach (Impacts imp in qImpacts)
                {
                    ((TreeViewItem)treeViewImpacts.Items[y]).Items.Add(SetTreeViewImpacts(imp.impt_name.ToString() ));
                    ((TreeViewItem)treeViewImpacts.Items[y]).IsExpanded = true;
                }
                y++;

            }

        }

        private TreeViewItem SetTreeViewImpacts(string text )
        {

            
            TreeViewItem item = new TreeViewItem();

            item.IsExpanded = true;

            StackPanel stack = new StackPanel();
            stack.Orientation = Orientation.Horizontal;


            Label lbl = new Label();
            lbl.Name = "lblImpText";
            lbl.Content = text;
            stack.Children.Add(lbl);

            item.Header = stack;

            item.Foreground = Brushes.Brown;
            item.BorderBrush = Brushes.Black;
            item.FontStyle = FontStyles.Italic;
            item.Cursor = Cursors.Hand;
            item.FontSize = 13;
            item.Height = 22;
           
            return item;
        }

        private void CloseButton_Click(object sender, RoutedEventArgs e)
        {
            if (dataGrid1.SelectedIndex > -1)
                ModelListe.RemoveAt(dataGrid1.SelectedIndex);
            else
                MessageBox.Show("Nothing has selected to remove", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
        }

        private void SaveButton_Click(object sender, RoutedEventArgs e)
        {

            if (isprojectSelected)
            {
                if (dataGrid1.Items.Count != 0)
                {
                    if (comboBoxComponents.SelectedIndex != -1)
                    {

                        List<String> Impc = new List<string>();
                        List<String> ImpcComp = new List<string>();
                        List<String> ImpcRef = new List<string>();
                        int rows = dataGrid1.Items.Count;
                        int rows1 = dataGrid1.Items.Count;
                        foreach (Impacts row in dataGrid1.Items)
                        {

                            Impc.Add(row.impt_name);
                            ImpcComp.Add(row.impt_component);
                            ImpcRef.Add(row.impt_reference);
                            rows--;
                            if (rows == 0)
                                break;
                        }


                        if (new cecbClasses.Data_Adapters.ImpactAndActivity.Func_Impacts().InsertImpact(ImpcComp, Impc, ImpcRef, rows1, labelProjectReference.Content.ToString()))
                        {
                            MessageBox.Show(dataGrid1.Items.Count + " Impacts inserted", "Message", MessageBoxButton.OK, MessageBoxImage.Information);
                            textBoxAddActivity.Text = "";
                            ModelListe.Clear();

                        }
                    }
                    else
                        MessageBox.Show("select the phace", "Error", MessageBoxButton.OK, MessageBoxImage.Error);

                }
                else MessageBox.Show("Add Impacts", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            }
            else MessageBox.Show("Select a project", "Error", MessageBoxButton.OK, MessageBoxImage.Error);



        }


        private void button1_Click(object sender, RoutedEventArgs e)
        {
            if (isprojectSelected)
            {

                if (comboBoxComponents.SelectedIndex != -1)
                {
                    if (textBoxAddActivity.Text.Length != 0)
                    {
                        model = new Impacts();

                       
                        
                        String proRef = ((Projects)ProjectAutocompleteTextBox.SelectedItem).proj_reference.ToString();
                        //if (new cecbClasses.Data_Adapters.ImpactAndActivity.Func_Impacts().InsertNewImpact(textBoxAddActivity.Text, comboBoxComponents.SelectedValue.ToString(),proRef))
                        //{
                        //    MessageBox.Show("Impact Added Successfully", "Message", MessageBoxButton.OK, MessageBoxImage.Information);
                            
                        //}

                        model.impt_name = textBoxAddActivity.Text;
                        model.impt_component = comboBoxComponents.Text;
                        model.impt_reference = new cecbClasses.Data_Adapters.ImpactAndActivity.Func_Impacts().InsertNewImpactReturnRef(textBoxAddActivity.Text, comboBoxComponents.SelectedValue.ToString(), proRef);
                        ModelListe.Add(model);
                         
                        textBoxAddActivity.Clear();

                    }
                    else MessageBox.Show("Please Enter a impact", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                }
                else MessageBox.Show("Please Select a component", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            }
            else MessageBox.Show("Please Select a project", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
        }


        private void textBoxAddActivity_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (textBoxAddActivity.Text.Length > 0)
            {

                (FindResource("drop") as Storyboard).Begin(this);
                listBoxComponents.ItemsSource = new cecbClasses.Data_Adapters.ImpactAndActivity.Func_Impacts().getImpactDetailsByName(textBoxAddActivity.Text.Trim());
                if (listBoxComponents.Items.Count > 0)
                {
                    groupBox.Visibility = System.Windows.Visibility.Visible;
                }
                else groupBox.Visibility = System.Windows.Visibility.Hidden;
            }
        }

        private void listBoxComponents_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (isprojectSelected)
            {

                List<Impacts> list = new List<Impacts>();

                if (listBoxComponents.SelectedIndex != -1)
                {
                    Impacts name = (Impacts)listBoxComponents.SelectedItem;
                    String x = name.impt_component;
                    DSSEntities cecbContext = new DSSEntities();
                    String y = cecbContext.Components.First(i => i.cmpt_reference == x).cmpt_name.ToString();

                    model = new Impacts();

                    //  model.impt_name= comboBoxComponents.Text;
                    try
                    {
                        model.impt_name = name.impt_name;
                        model.impt_component = y;
                        model.impt_reference = name.impt_reference;
                        ModelListe.Add(model);
                    }
                    catch (Exception)
                    {

                    }
                }
            }
            else
            {

                MessageBox.Show("Select a project to add Impacts", "Message", MessageBoxButton.OK, MessageBoxImage.Error);
            }

        }

        private void textBoxAddActivity_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            if (comboBoxComponents.SelectedIndex < 0)
            {
                MessageBox.Show("Select a phase", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                e.Handled = true;
            }
        }

        private void textBoxActivitySearch_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (textBoxActivitySearch.Text.Length > 0)
            {

                (FindResource("drop") as Storyboard).Begin(this);
                listBoxComponents.ItemsSource = new cecbClasses.Data_Adapters.ImpactAndActivity.Func_Impacts().getImpactDetailsByName(textBoxAddActivity.Text.Trim());
                if (listBoxComponents.Items.Count > 0)
                {
                    groupBox.Visibility = System.Windows.Visibility.Visible;
                }
                else groupBox.Visibility = System.Windows.Visibility.Hidden;
            }
        }

        private void textBoxActivitySearch_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            if (!isprojectSelected)
            {
                MessageBox.Show("Select a project", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                e.Handled = true;
            }
            
        }

        private void Image_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.Close();
        }

        private void comboBoxComponents_DropDownOpened(object sender, EventArgs e)
        {
            if (!isprojectSelected) {
                MessageBox.Show("Select a project", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        
    }
}
